import request from "@/utils/request";

function joinAddress(computerInfo) {
  return `http://${computerInfo.ip}:${computerInfo.port}`;
}

/**
 *
 *  设备详情（同时判断设备是否在线）
 */
export function getComputerInfo(computerInfo) {
  return request.get({
    url: joinAddress(computerInfo) + "/control/computer_info",
    headers: {
      "X-Access-Key": computerInfo.key,
    },
    timeout: 5000,
  });
}

/**
 *
 *  设备关闭指令
 */
export function turnOffCommand(computerInfo) {
  return request.post({
    url: joinAddress(computerInfo) + "/control/preset_command",
    data: {
      command: "turn_off",
    },
    headers: {
      "X-Access-Key": computerInfo.key,
    },
  });
}

/**
 *
 *  设备重启指令
 */
export function restartCommand(computerInfo) {
  return request.post({
    url: joinAddress(computerInfo) + "/control/preset_command",
    data: {
      command: "restart",
    },
    headers: {
      "X-Access-Key": computerInfo.key,
    },
  });
}

/**
 *
 *  设备息屏指令
 */
export function lockOnCommand(computerInfo) {
  return request.post({
    url: joinAddress(computerInfo) + "/control/preset_command",
    data: {
      command: "lock_on",
    },
    headers: {
      "X-Access-Key": computerInfo.key,
    },
  });
}

/**
 *
 *  设备息屏指令
 */
export function screenOffCommand(computerInfo) {
  return request.post({
    url: joinAddress(computerInfo) + "/control/preset_command",
    data: {
      command: "screen_off",
    },
    headers: {
      "X-Access-Key": computerInfo.key,
    },
  });
}

/**
 *
 *  设备自定义指令
 */
export function sendCustomCommand(computerInfo, customCommand) {
  return request.post({
    url: joinAddress(computerInfo) + "/control/command",
    data: {
      content: customCommand.content,
      permission: customCommand.permission,
    },
    headers: {
      "X-Access-Key": computerInfo.key,
    },
  });
}

/**
 *
 *  设备自定义脚本
 */
export function sendCustomScript(computerInfo, customCommand) {
  return request.post({
    url: joinAddress(computerInfo) + "/control/script",
    data: {
      script_id: customCommand.script_id,
      script_type: customCommand.script_type,
      content: customCommand.content,
      permission: customCommand.permission,
    },
    headers: {
      "X-Access-Key": computerInfo.key,
    },
  });
}
